package com.maxlen.ware.listener;

import com.maxlen.ware.entity.WareOrderTaskDetailEntity;
import com.maxlen.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "ware.stock.release.queue")
public class WareStockRabbitListener {

    @Autowired
    private WareSkuService wareSkuService;

    @RabbitHandler
    public void wareStockReleaseQueue(Long wareOrderTaskDetailId, @Headers Message headers, Channel channel) throws IOException {
        System.out.println("开始解锁库存.....");
        try{
            wareSkuService.isUnlockStock(wareOrderTaskDetailId);
            channel.basicAck(headers.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            System.out.println(e);
            channel.basicReject(headers.getMessageProperties().getDeliveryTag(), true);
        }
    }

    @RabbitHandler
    public void wareStockReleaseQueue(String orderSn, @Headers Message headers, Channel channel) throws IOException {
        System.out.println("订单取消开始解锁库存.....");
        try{
            wareSkuService.isUnlockStock(orderSn);
            channel.basicAck(headers.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            System.out.println(e);
            channel.basicReject(headers.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
